﻿using SevenZip;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class mapoffset_GZIP : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        //offset 路径
        string path = Server.MapPath("./offset.xMap");
        //压缩
        Util.Instance.CompressFiles(path, path + ".7z");

        System.IO.Stream sr = System.IO.File.Create(path + "ex");

        //内存流
        sr = new MemoryStream();

        //获取压缩后的数据文件名
        string flname = string.Empty;
        //解压缩
        Util.Instance.Decompress(path + ".7z", sr,ref flname);
        sr.Dispose();
        sr = null;
   



    }
    public class Util
    {
        private static readonly Util instance = new Util();
        /// <summary>
        /// 数据访问实例
        /// </summary>
        public static Util Instance
        {
            get { return instance; }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        protected Util()
        {
            //当前程序是否64伴运行
            if (Environment.Is64BitProcess)
            {
                //设置7z运行库
                SevenZipCompressor.SetLibraryPath(AppDomain.CurrentDomain.BaseDirectory + ("7zip/7z64.dll"));
                SevenZipExtractor.SetLibraryPath(AppDomain.CurrentDomain.BaseDirectory + ("7zip/7z64.dll"));
            }
            else
            {
                //设置7z运行库
                SevenZipCompressor.SetLibraryPath(AppDomain.CurrentDomain.BaseDirectory + ("7zip/7z.dll"));
                SevenZipExtractor.SetLibraryPath(AppDomain.CurrentDomain.BaseDirectory + ("7zip/7z.dll"));
            }
        }



        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="sourceFile">需要压缩的文件</param>
        /// <param name="tagzipFile">压缩后的目标文件</param>
        public void CompressFiles(string sourceFile, string tagzipFile)
        {
            SevenZipCompressor tmp = new SevenZipCompressor();
            //输出目标文件类型
            tmp.ArchiveFormat = OutArchiveFormat.SevenZip;

            //进行压缩文件
            tmp.CompressFiles(tagzipFile, sourceFile);
            tmp = null;
            //目录压缩
            //tmp.CompressDirectory(@"c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\1033", @"D:\Temp\arch.7z");
        }

        /// <summary>
        /// 解压缩单个文件
        /// </summary>
        /// <param name="sourceFile">需要解压缩的文件</param>
        /// <param name="tagzipFile">解压缩后的目标文件流</param>
        public void Decompress(string sourcezipFile, Stream outPutStream, ref string fileName)
        {

            using (SevenZipExtractor zip = new SevenZipExtractor(sourcezipFile))
            {
                try
                {
                    if (zip.ArchiveFileData.Count > 0)
                    {
                        //当前解压的文件名
                        fileName = zip.ArchiveFileNames[0];
                        zip.ExtractFile(0, outPutStream);
                    }
                }
                finally
                {
                    if (outPutStream != null)
                    {
                        outPutStream.Flush();
                        outPutStream.Close();
                    }
                }
            }
        }

 
    }


}